//
//  GMProtectTool.m
//  gentleman
//
//  Created by 范月成 on 2017/12/22.
//  Copyright © 2017年 fancy. All rights reserved.
//

#import "GMProtectTool.h"
#import <LocalAuthentication/LocalAuthentication.h>

@implementation GMProtectTool

static GMProtectTool *instance = nil;
+ (instancetype)shareTool {
    return [[self alloc] init];
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [super allocWithZone:zone];
    });
    return instance;
}

- (instancetype)init {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [super init];
    });
    return instance;
}

- (id)copyWithZone:(NSZone *)zone {
    return  instance;
}

+ (id)copyWithZone:(struct _NSZone *)zone {
    return  instance;
}

- (BOOL)isEnableTouchID {
    LAContext *laContext = [LAContext new];
    NSError *error;
    return [laContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:&error];
}

- (void)recordEnterBackgroundTime:(NSTimeInterval)time {
    _time = time;
}

- (void)showTouchID:(completBlock)completion {
    LAContext *laContext = [LAContext new];
    NSError *error;
    if ([laContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:&error]) {
        [laContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
                  localizedReason:@"To Be Gentle"
                            reply:^(BOOL success, NSError *error) {
                                dispatch_async(dispatch_get_main_queue(), ^{
                                    if (success) {
                                        !completion?:completion(GMProtectStatusSuccess);
                                    }
                                    if (error) {
                                        !completion?:completion(GMProtectStatusCannel);
                                    }
                                });
                            }];
    }
}

@end
